package com.ybxx.dashboard.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.StatementCallback;

import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.impl.BaseDaoImpl;
import com.ybxx.core.web.paging.PagingBean;
import com.ybxx.dashboard.dao.WatermeterDao;
import com.ybxx.dashboard.model.CopyFormVO;
import com.ybxx.dashboard.model.ReadFormVO;
import com.ybxx.dashboard.model.Watermeter;

public class WatermeterDaoImpl extends BaseDaoImpl<Watermeter> implements
		WatermeterDao {

	public WatermeterDaoImpl() {
		super(Watermeter.class);
		// TODO Auto-generated constructor stub
	}
	/*查询水表读数*/
	public List<ReadFormVO> queryReadForm(final String sql,final String printtype){
		return super.jdbcTemplate.execute(new StatementCallback<List<ReadFormVO>>(){
			public List<ReadFormVO> doInStatement(Statement statement){
				List<ReadFormVO> list=new ArrayList<ReadFormVO>();
				try {
					ResultSet rs=statement.executeQuery(sql);
					String snum_sign="";
					ReadFormVO vo=null;
					while(rs.next()){
						
						if(!snum_sign.equals(rs.getString("snum"))){
							vo=new ReadFormVO();
							vo.setCode(rs.getString("acode"));
							vo.setSnum(rs.getString("snum"));
							vo.setThereadings(rs.getString("thereadings"));
							vo.setCurrentreading(rs.getString("currentreading"));
							vo.setRids(rs.getString("rid"));
							vo.setCompares(rs.getString("compare"));
							vo.setAmrcodes(rs.getString("amrcode"));
							vo.setWalkquantity(rs.getString("walkquantity"));
							vo.setAmount(rs.getString("amount"));
							vo.setCharge(rs.getString("charge"));
							vo.setStates(rs.getString("state"));
							snum_sign=rs.getString("snum");
							list.add(vo);
						}else{
							vo.setRids(vo.getRids()+","+rs.getString("rid"));
							vo.setCompares(vo.getCompares()+","+rs.getString("compare"));
							vo.setAmrcodes((vo.getAmrcodes()==null || vo.getAmrcodes().equals(""))?rs.getString("amrcode"):vo.getAmrcodes()+","+rs.getString("amrcode"));
							vo.setStates(vo.getStates()+","+rs.getString("state"));
						}
						vo.setState((vo.getStates()==null || vo.getStates().indexOf("1")<0)?"0":"1");
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return list;
			}
		});
	}
	/*查询电表抄表单*/
	public Map<String,List<CopyFormVO>> queryCopyForm(final String sql,final String printtype){
		return super.jdbcTemplate.execute(new StatementCallback<Map<String,List<CopyFormVO>>>(){
			public Map<String,List<CopyFormVO>> doInStatement(Statement statement){
				Map<String,List<CopyFormVO>> map=new HashMap<String,List<CopyFormVO>>();
				try {
					ResultSet rs=statement.executeQuery(sql);
					String sign="";
					List<CopyFormVO> list=null;
					String columnName="";
					if(printtype==null || "".equals(printtype) || "unit".equals(printtype)){
						columnName="roomcode";
					}else{
						columnName="cbname";
					}
					int i=0;
					while(rs.next()){
						String tmp=rs.getString(columnName);
						String text=tmp.substring(tmp.indexOf("(")<0?0:tmp.indexOf("(")+1,tmp.indexOf(")")<0?tmp.length():tmp.indexOf(")"));
						if(!sign.equals(text)){
							list=new ArrayList<CopyFormVO>();
							map.put(i+","+text,list);
							sign=text;
							i++;
						}
						CopyFormVO vo=new CopyFormVO();
						vo.setPname(rs.getString("pname"));
						vo.setSnum(rs.getString("snum"));
						vo.setThereadings(rs.getString("thereadings"));
						vo.setCurrentreading(rs.getString("currentreading"));
						vo.setRoom(rs.getString("roomcode"));
						vo.setTel(rs.getString("tel"));
						list.add(vo);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return map;
			}
		});
	}
}
